﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AppHelper
{
    public partial class RegForm : Form
    {
        public AppLicense license = new AppLicense();
        public RegForm()
        {
            InitializeComponent();
        }

        private void RegForm_Load(object sender, EventArgs e)
        {
            DialogResult = DialogResult.None;

            String msg = "";
            if(license.CheckLicense(ref msg))
            {
                DialogResult = DialogResult.OK;
                Close();    
            }
            else
            {
                Text = msg;
                textBox1.Text = license.GetLicense();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(textBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String msg = "";
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.FileName = "license.dat";
            ofd.Filter = "文本文件(*.dat)|*.dat|所有文件(*.*)|*.*";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            { 
                if (license.CheckLicense(ofd.FileName,ref msg))
                {
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    MessageBox.Show(msg,"注册码无效");
                    textBox1.Text = license.GetLicense();
                }
            }
        }

        private void textBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                this.textBox1.Cursor = System.Windows.Forms.Cursors.Arrow;  //指定鼠标形状（更好看）
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }

        }

        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            String msg = "";
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            if (license.CheckLicense(path, ref msg))
            {
                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                MessageBox.Show(msg, "注册码无效");
                textBox1.Text = license.GetLicense();
            }
            this.textBox1.Cursor = System.Windows.Forms.Cursors.IBeam; //还原鼠标形状
        }
    }
}
